﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace 掌控校园
{
    public partial class SignInPage : PhoneApplicationPage
    {
        public static string postback = "";
        public static string postdata = "";

        public SignInPage()
        {
            InitializeComponent();
        }


        private void SignInButton_Click(object sender, RoutedEventArgs e)
        {
            ProgressBar.Opacity = 1;
            //验证
            if(ID.Text=="")
            {
                MessageBox.Show("学号不能为空!");
                return;
            }
            else if(Password.Password=="")
            {
                MessageBox.Show("密码不能为空！");
                return;
            }
            else
            {
                postdata = "user=" + ID.Text + "&upwd=" + Register.MD5_BASE64(Password.Password);              //Password.Password;
                //postdata = "user=201213030316&upwd=123123123";
                //verify ueser 
                string uri = "http://202.115.137.129/zhangkong/login.php";
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.BeginGetRequestStream(StreamCallback, httpWebRequest);
            }
            






            //这里需要异步，就是你肯定懂
            ProgressBar.Opacity = 0;



        }

        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Register.xaml?IsRegister=true", UriKind.Relative));
        }

        private void GoToMainPageWithoutSignIn_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/CoursePage.xaml", UriKind.Relative));
        }

        //下面进行回调函数
        ///////////////////
        private void StreamCallback(IAsyncResult result)
        {

            HttpWebRequest httpWebRequest = (HttpWebRequest)result.AsyncState;
            using (Stream stream = httpWebRequest.EndGetRequestStream(result))
            {
                byte[] entryBytes = Encoding.UTF8.GetBytes(postdata);
                stream.Write(entryBytes, 0, entryBytes.Length);
            }

            //这里就和上面GET方法获得回复回调一样，用 private void ResponseCallback(IAsyncResult result) 这个方法。
            httpWebRequest.BeginGetResponse(ResponseCallback, httpWebRequest);
        }
        //第二个回调函数
        private void ResponseCallback(IAsyncResult result)
        {
            Stream stream = null;

            try
            {
                //获取异步操作返回的的信息 
                HttpWebRequest httpWebRequest = (HttpWebRequest)result.AsyncState;
                //结束对 Internet 资源的异步请求
                WebResponse webResponse = httpWebRequest.EndGetResponse(result);
                stream = webResponse.GetResponseStream();
                //StreamReader sr = new StreamReader(stream, System.Text.UTF8Encoding.UTF8);
                using (StreamReader sr = new StreamReader(stream))
                {
                    postback = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(ex.Message, "异常", MessageBoxButton.OK);
                });

            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            if (postback.Length == 5)
            {
                //因为HttpWebRequest是异步，不在UI线程上。所以要改变UI线程上的控件属性就要用Dispatcher.BeginInvoke()。  
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(postback, "登录", MessageBoxButton.OK);
                });
            }
            else
            {
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri("/CoursePage.xaml", UriKind.Relative));
                });
            }


        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            try
            {
                ID.Text = Convert.ToString(IsolatedStorageSettings.ApplicationSettings["StudentID"]);
                Password.Password = Convert.ToString(IsolatedStorageSettings.ApplicationSettings["Password"]);
            }
            catch (Exception)
            {

            }

        }


        //下面是类尾
    }
}